package com.book.common.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.book.business.entity.BookSell;
import com.book.business.service.BookSellService;
import com.book.shelves.entity.BookStore;
import com.book.shelves.service.BookStoreService;
import com.book.warehouse.entity.WarehouseBook;
import com.book.warehouse.entity.WarehouseHouse;
import com.book.warehouse.entity.WarehouseHouseBook;
import com.book.warehouse.service.WarehouseBookService;
import com.book.warehouse.service.WarehouseHouseBookService;
import com.book.warehouse.service.WarehouseHouseService;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: Zhang Gaohe
 * @Date: 2021/5/9
 **/

@Component
public class WareBook {
    // 书籍bookname 书名，message 类型，number 数量
    public static void wareBookChange(String bookname, String message, Integer number) {
        BookStoreService bookStoreService = SpringContextUtil.getBean(BookStoreService.class);
        BookSellService bookSellService = SpringContextUtil.getBean(BookSellService.class);
        WarehouseBookService warehouseBookService = SpringContextUtil.getBean(WarehouseBookService.class);
        WarehouseHouseService warehouseHouseService = SpringContextUtil.getBean(WarehouseHouseService.class);
        WarehouseHouseBookService warehouseHouseBookService = SpringContextUtil.getBean(WarehouseHouseBookService.class);
        BookStore bookStore = bookStoreService.getOne(new QueryWrapper<BookStore>().lambda().eq(BookStore::getBookname, bookname).notIn(BookStore::getFlag, "1"));
        BookSell bookSell = bookSellService.getOne(new QueryWrapper<BookSell>().lambda().eq(BookSell::getBookname, bookname));
        WarehouseBook serviceOne = warehouseBookService.getOne(new UpdateWrapper<WarehouseBook>().lambda().eq(WarehouseBook::getBookname, bookname).eq(WarehouseBook::getIntoware, "1"));
        List<WarehouseHouseBook> warehouseHouseBookList = warehouseHouseBookService.list(new QueryWrapper<WarehouseHouseBook>().lambda()
                .eq(WarehouseHouseBook::getBookid, serviceOne.getBookid()));
        for (int j = 0; j < warehouseHouseBookList.size(); j++) {
            WarehouseHouse house1 = warehouseHouseService.getOne(new QueryWrapper<WarehouseHouse>().lambda().eq(WarehouseHouse::getHouseid, warehouseHouseBookList.get(j).getHouseid()));
            if ("减少".equals(message)) {
                bookStore.setNum(bookStore.getNum() - number);
                bookSell.setNumber(bookSell.getNumber() - number);
                serviceOne.setNumber(serviceOne.getNumber() - number);
                // 本仓库书籍数量满足
                if (warehouseHouseBookList.get(j).getHbnumber() > number) {
                    house1.setHousebooks(house1.getHousebooks() - number);
                    warehouseHouseService.updateWarehouseHouse(house1);
                    warehouseHouseBookList.get(j).setHbnumber(warehouseHouseBookList.get(j).getHbnumber() - number);
                    warehouseHouseBookService.updateWarehouseHouseBook(warehouseHouseBookList.get(j));
                    break;
                }
                // 本仓库书籍数量刚刚好
                if (warehouseHouseBookList.get(j).getHbnumber().equals(number)) {
                    house1.setHousebooks(house1.getHousebooks() - number);
                    warehouseHouseService.updateWarehouseHouse(house1);
                    warehouseHouseBookService.removeById(warehouseHouseBookList.get(j).getBusinessid());
                    break;
                }
                // 本仓库书籍数量不足
                if (warehouseHouseBookList.get(j).getHbnumber() < number) {
                    house1.setHousebooks(house1.getHousebooks() - number);
                    warehouseHouseService.updateWarehouseHouse(house1);
                    if (warehouseHouseBookList.size() > 1) {
                        WarehouseHouse house2 = warehouseHouseService.getOne(new QueryWrapper<WarehouseHouse>().lambda().eq(WarehouseHouse::getHouseid, warehouseHouseBookList.get(j + 1).getHouseid()));
                        house2.setHousebooks(house2.getHousebooks() + warehouseHouseBookList.get(j).getHbnumber() - number);
                        warehouseHouseService.updateWarehouseHouse(house2);
                    }
                    warehouseHouseBookList.get(j + 1).setHbnumber(warehouseHouseBookList.get(j).getHbnumber() + warehouseHouseBookList.get(j + 1).getHbnumber() - number);
                    warehouseHouseBookService.removeById(warehouseHouseBookList.get(j).getBusinessid());
                    warehouseHouseBookService.updateWarehouseHouseBook(warehouseHouseBookList.get(j + 1));
                    break;
                }
            }
            if ("增加".equals(message)) {
                serviceOne.setNumber(serviceOne.getNumber() + number);
                if (bookSell == null) {
                    bookSell = new BookSell()
                            .setBookname(serviceOne.getBookname())
                            .setPubid(serviceOne.getPurchasefactory())
                            .setType(serviceOne.getType())
                            .setBookmoney(serviceOne.getBookmoney())
                            //因为前端没有接收书籍信息，所以从数据库拿
                            .setMessage(BookMethod.message(serviceOne.getType()))
                            .setNumber(serviceOne.getNumber())
                            .setCart(0)
                            .setLove(0);
                    bookStore = new BookStore()
                            .setBookname(serviceOne.getBookname())
                            .setPubid(serviceOne.getPurchasefactory())
                            .setType(serviceOne.getType())
                            .setBookmoney(serviceOne.getBookmoney())
                            .setMessage(BookMethod.message(serviceOne.getType()))
                            .setNum(serviceOne.getNumber())
                            .setFlag("0");
                } else {
                    bookStore.setNum(bookStore.getNum() + number);
                    bookSell.setNumber(bookSell.getNumber() + number);
                }
                // 本仓库书籍数量空余满足
                if (house1.getHousenumber() - house1.getHousebooks() >= number) {
                    warehouseHouseBookList.get(j).setHbnumber(warehouseHouseBookList.get(j).getHbnumber() + number);
                    warehouseHouseBookService.updateWarehouseHouseBook(warehouseHouseBookList.get(j));
                    house1.setHousebooks(house1.getHousebooks() + number);
                    warehouseHouseService.updateWarehouseHouse(house1);
                    break;
                }
                // 本仓库书籍数量空余不足
                if (house1.getHousenumber() - house1.getHousebooks() < number) {
                    warehouseHouseBookList.get(j).setHbnumber(warehouseHouseBookList.get(j).getHbnumber() + house1.getHousenumber() - house1.getHousebooks());
                    warehouseHouseBookService.updateWarehouseHouseBook(warehouseHouseBookList.get(j));
                    warehouseHouseBookList.get(j + 1).setHbnumber(warehouseHouseBookList.get(j + 1).getHbnumber() + house1.getHousebooks() + number - house1.getHousenumber());
                    warehouseHouseBookService.updateWarehouseHouseBook(warehouseHouseBookList.get(j + 1));
                    house1.setHousebooks(house1.getHousenumber());
                    warehouseHouseService.updateWarehouseHouse(house1);
                    if (warehouseHouseBookList.size() > 1) {
                        WarehouseHouse house2 = warehouseHouseService.getOne(new QueryWrapper<WarehouseHouse>().lambda().eq(WarehouseHouse::getHouseid, warehouseHouseBookList.get(j + 1).getHouseid()));
                        house2.setHousebooks(house2.getHousebooks() + house1.getHousebooks() + number - house1.getHousenumber());
                        warehouseHouseService.updateWarehouseHouse(house2);
                    }
                    break;
                }
            }
            if ("同步".equals(message)) {
                serviceOne.setNumber(serviceOne.getNumber() + number);
                if (bookSell == null) {
                    bookSell = new BookSell()
                            .setBookname(serviceOne.getBookname())
                            .setPubid(serviceOne.getPurchasefactory())
                            .setType(serviceOne.getType())
                            .setBookmoney(serviceOne.getBookmoney())
                            //因为前端没有接收书籍信息，所以从数据库拿
                            .setMessage(BookMethod.message(serviceOne.getType()))
                            .setNumber(serviceOne.getNumber() + number)
                            .setCart(0)
                            .setLove(0);
                    bookStore = new BookStore()
                            .setBookname(serviceOne.getBookname())
                            .setPubid(serviceOne.getPurchasefactory())
                            .setType(serviceOne.getType())
                            .setBookmoney(serviceOne.getBookmoney())
                            .setMessage(BookMethod.message(serviceOne.getType()))
                            .setNum(serviceOne.getNumber() + number)
                            .setFlag("0");
                } else {
                    bookStore.setNum(serviceOne.getNumber() + number);
                    bookSell.setNumber(serviceOne.getNumber() + number);
                }
                // 本仓库书籍数量空余满足
                if (house1.getHousenumber() - house1.getHousebooks() >= number) {
                    warehouseHouseBookList.get(j).setHbnumber(warehouseHouseBookList.get(j).getHbnumber() + number);
                    warehouseHouseBookService.updateWarehouseHouseBook(warehouseHouseBookList.get(j));
                    house1.setHousebooks(house1.getHousebooks() + number);
                    warehouseHouseService.updateWarehouseHouse(house1);
                    break;
                }
                // 本仓库书籍数量空余不足
                if (house1.getHousenumber() - house1.getHousebooks() < number) {
                    warehouseHouseBookList.get(j).setHbnumber(warehouseHouseBookList.get(j).getHbnumber() + house1.getHousenumber() - house1.getHousebooks());
                    warehouseHouseBookService.updateWarehouseHouseBook(warehouseHouseBookList.get(j));
                    warehouseHouseBookList.get(j + 1).setHbnumber(warehouseHouseBookList.get(j + 1).getHbnumber() + house1.getHousebooks() + number - house1.getHousenumber());
                    warehouseHouseBookService.updateWarehouseHouseBook(warehouseHouseBookList.get(j + 1));
                    house1.setHousebooks(house1.getHousenumber());
                    warehouseHouseService.updateWarehouseHouse(house1);
                    if (warehouseHouseBookList.size() > 1) {
                        WarehouseHouse house2 = warehouseHouseService.getOne(new QueryWrapper<WarehouseHouse>().lambda().eq(WarehouseHouse::getHouseid, warehouseHouseBookList.get(j + 1).getHouseid()));
                        house2.setHousebooks(house2.getHousebooks() + house1.getHousebooks() + number - house1.getHousenumber());
                        warehouseHouseService.updateWarehouseHouse(house2);
                    }
                    break;
                }
            }
        }
        bookStoreService.updateBookStore(bookStore);
        bookSellService.updateBookSell(bookSell);
        warehouseBookService.updateWarehouseBook(serviceOne);
    }
}